import httpService from "./service/httpService";
import { ElLoading, dayjs, ElMessage } from "element-plus";
import { saveAs } from "file-saver";

export default httpService;

export * from "./modules/monitor/logininfor";
export * from "./modules/monitor/online";
export * from "./modules/monitor/operlog";
export * from "./modules/monitor/server";

export * from "./modules/system/config";
export * from "./modules/system/dept";
export * from "./modules/system/dict";
export * from "./modules/system/login";
export * from "./modules/system/menu";
export * from "./modules/system/post";
export * from "./modules/system/role";
export * from "./modules/system/user";

/**
 * 通用下载方法
 * @param fn 请求回调
 * @param fileName 文件名称 注意添加后缀
 * @returns
 */
export const download = async (fn: () => Promise<any>, fileName: string) => {
  const downloadLoadingIns = ElLoading.service({
    text: "下载中...",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)"
  });
  try {
    if (!fn || typeof fn !== "function") return;
    if (!fileName) {
      fileName = dayjs(new Date()).format("YYYYMMDDHHmmss") + ".xlsx";
    }
    fn().then((res: any) => {
      const blob = new Blob([res]);
      saveAs(blob, fileName);
    });
  } catch (error) {
    ElMessage.error("下载出错，请联系管理员！");
  } finally {
    downloadLoadingIns.close();
  }
};
